Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Do-while Loop

do-while loop examples

Here are some examples of do-while loops in Java along with their expected outputs:

Example 1: Simple Counting

Simple Counting using do-while loop public class Main{ public static void main(String[] args) { int count = 1; do { System.out.println("Count: " + count); count++; } while (count <= 5); } }

Output

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
The above java example shows how to count numbers using do-while loop untill count becomes 5. once it is equal to 5 or greater than 5 then the condition fails and break the loop.

Example 2: User Input Validation

User Input Validation
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; do { System.out.print("Enter a number (greater than 5): "); number = scanner.nextInt(); } while (number <= 5); System.out.println("You entered a number greater than 5: " + number); } }
Output (if the user enters 4, then 6):

Output

Enter a number (greater than 5): 4 Enter a number (greater than 5): 6 You entered a number greater than 5: 6
The above java example shows how to Validate users input using do-while loop. once the user enters number equal to 5 or greater than 5 then the condition fails and print the output as "You entered a number greater than 5: " + numberYouEntered". If user enter less than 5 the condition will continue until the user enters greater than 5.

Example 3: Countdown Timer

Countdown Timer
public class Main{ public static void main(String[] args) { int seconds = 10; do { System.out.println("Time left: " + seconds + " seconds"); seconds--; } while (seconds >= 0); } }

Output

Time left: 10 seconds Time left: 9 seconds Time left: 8 seconds Time left: 7 seconds Time left: 6 seconds Time left: 5 seconds Time left: 4 seconds Time left: 3 seconds Time left: 2 seconds Time left: 1 seconds Time left: 0 seconds
The above java example shows how to create a timer Countdown using do-while loop. The seconds variable has a value of 10 and it is decremented after every output in do. once the seconds become zero the loop discontinued.

Example 4: Rolling a Die Until You Get a 6
Rolling a Die Until You Get a 6
import java.util.Random; public class Main{ public static void main(String[] args) { Random rand = new Random(); int roll; int attempts = 0; do { roll = rand.nextInt(6) + 1; // Simulate rolling a six-sided die System.out.println("Roll #" + (attempts + 1) + ": " + roll); attempts++; } while (roll != 6); System.out.println("It took " + attempts + " rolls to get a 6."); } }
Output (the number of rolls will vary):

Output

Roll #1: 3 Roll #2: 6 It took 2 rolls to get a 6.
In this example, the program uses the Random class to simulate rolling a six-sided die (numbers 1 to 6). The do-while loop continues to roll the die until a 6 is rolled. It prints the result of each roll and increments the attempts counter. Once a 6 is rolled, the loop terminates, and it displays the number of attempts it took to get a 6.

Example 5: Sum of Numbers Entered by the User

Sum of Numbers Entered
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; int number; do { System.out.print("Enter a number (0 to quit): "); number = scanner.nextInt(); sum += number; } while (number != 0); System.out.println("The sum of the numbers entered is: " + sum); } }
Output (user-entered numbers, for example, 5, 8, 3, 0):

Output

Enter a number (0 to quit): 5 Enter a number (0 to quit): 8 Enter a number (0 to quit): 3 Enter a number (0 to quit): 0 The sum of the numbers entered is: 16
The above java example shows how to sum the numbers which was entered by user using do-while loop. when user enters number greater than 0. it will be stored in a number variable and added to sum variables. Each time when user enters input it will be assigned to number and added to sum and once the user enters zero, the loop will break as the condtion passes. These examples demonstrate different use cases of do-while loops in Java.

  📌TAGS

★do-while ★for ★while ★loop ★for-each

Tutorials